package com.jlee.android.pimclient.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

import com.jlee.android.pimclient.PIMClientConstant;
import com.jlee.android.pimclient.R;

public class PIMClientCalendarSettingActivity extends Activity {
	CheckBox mTitleCB;
	CheckBox mDescriptionCB;
	CheckBox mLocationCB;
	CheckBox mStartTimeCB;
	CheckBox mEndTimeCB;
	
	SharedPreferences mSettings;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.calendar_setting);
		setTitle(R.string.title_calendar_setting);

		mSettings = getSharedPreferences(PIMClientConstant.PREFS_NAME, MODE_PRIVATE);

		mTitleCB = (CheckBox) findViewById(R.id.eventTitle);
		mDescriptionCB = (CheckBox) findViewById(R.id.eventDescription);
		mLocationCB = (CheckBox) findViewById(R.id.eventLocation);
		mStartTimeCB = (CheckBox) findViewById(R.id.eventStartTime);
		mEndTimeCB = (CheckBox) findViewById(R.id.eventEndTime);

		final Button saveButton = (Button) findViewById(R.id.save);
		saveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				doSave();
			}
			
		});
		
		initChecked();
	}

	private void initChecked() {
		mTitleCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CALENDAR_TITLE, true));
		mDescriptionCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CALENDAR_DESCRIPTION, true));
		mLocationCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CALENDAR_LOCATION, true));
		mStartTimeCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CALENDAR_STARTTIME, true));
		mEndTimeCB.setChecked(mSettings.getBoolean(
				PIMClientConstant.PREFS_KEY_CALENDAR_ENDTIME, true));
	}
	
	void doSave() {
		Editor editor = mSettings.edit();

		editor.putBoolean(PIMClientConstant.PREFS_KEY_CALENDAR_TITLE, 
				mTitleCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CALENDAR_DESCRIPTION, 
				mDescriptionCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CALENDAR_LOCATION, 
				mLocationCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CALENDAR_STARTTIME, 
				mStartTimeCB.isChecked());
		editor.putBoolean(PIMClientConstant.PREFS_KEY_CALENDAR_ENDTIME, 
				mEndTimeCB.isChecked());

		editor.commit();
		finish();
	}
}
